<?php

class Calendar extends CI_Controller {

	static $template = '
   {table_open}<table border="0" cellpadding="1" cellspacing="5">{/table_open}

   {heading_row_start}<tr>{/heading_row_start}

   {heading_previous_cell}<th><a href="{previous_url}">&lt;&lt;</a></th>{/heading_previous_cell}
   {heading_title_cell}<th colspan="{colspan}"><div class="cal_heading">{heading}</div></th>{/heading_title_cell}
   {heading_next_cell}<th><a href="{next_url}">&gt;&gt;</a></th>{/heading_next_cell}

   {heading_row_end}</tr>{/heading_row_end}

   {week_row_start}<tr>{/week_row_start}
   {week_day_cell}<td>{week_day}</td>{/week_day_cell}
   {week_row_end}</tr>{/week_row_end}

   {cal_row_start}<tr>{/cal_row_start}
   {cal_cell_start}<td>{/cal_cell_start}

   {cal_cell_content} <a href="{content}" class="cal_event">{day}</a>{/cal_cell_content}
   {cal_cell_content_today}<div class="highlight"><a href="{content}">{day}</a></div>{/cal_cell_content_today}

   {cal_cell_no_content}{day}{/cal_cell_no_content}
   {cal_cell_no_content_today}<div class="highlight">{day}</div>{/cal_cell_no_content_today}

   {cal_cell_blank}&nbsp;{/cal_cell_blank}

   {cal_cell_end}</td>{/cal_cell_end}
   {cal_row_end}</tr>{/cal_row_end}

   {table_close}</table>{/table_close}';

	function __construct()
	{
		parent::__construct();
	}

	/**
		Default calendar page,
		loads the calendara at the current date
		*/
	function index() {
		$date = getdate();
		$calendar = $this->show($date['year'], $date['mon']);

	}

	/**
	 * Shows the calendar
	 * */
	function show($year, $month){
		$prefs = array (
			'start_day' => 'monday',
			'month_type' => 'long',
			'day_type' => 'short',
			'show_next_prev' => true,
			'next_prev_url' => base_url() .'calendar/show/' );

		$prefs['template'] = Calendar::$template;

		$this->load->model('calendar_model');
		$event_list = $this->calendar_model->get_events($year, $month);
		$events = array();

		if ($event_list != FALSE){
			foreach($event_list as $e){
				$unix_time = strtotime($e['date']);
				if ($unix_time != false){
					$date = getdate($unix_time);
					$events[$date['mday']] = '#';
				}
			}
		}

		$this->load->library('calendar', $prefs);
		$calendar =$this->calendar->generate($year, $month, $events);

		$params['events'] = $event_list;
		$params['subview'] = 'subview/calendar_view';
		$params['calendar'] = $calendar;
		$this->load->view('homepage_view', $params);
	}

	/**
	 * jumps to a different date on the calendar.
	 * Through a form.
	 *
	 */
	function jump(){
		$year = $this->input->post('year');
		$month = $this->input->post('month');

		$this->show($year, $month);
	}

	/**
	 * Goes to the add event page
	 */
	function add_event(){
		$params['subview'] = 'subview/calendar_add_view';
		$this->load->view('homepage_view', $params);
	}

	/**
	 * Opens the add event page to edit an existing event entry
	 *
	 */
	function edit($id = 0){
		if ($this->gateway_model->is_admin() && $id != 0){
			$this->load->model('calendar_model');
			$event = $this->calendar_model->get_event($id);
			if (isset($event)){
				$params['event'] = $event;
				$params['subview'] = 'subview/calendar_add_view';
				$this->load->view('homepage_view', $params);
			}
		} else {
			redirect('/calendar/');
		}
	}

	/**
	 * Updates changes to an event
	 */
	function change(){
		if ($this->validate_event()){
			$data = array (
				'id' => $this->input->post('id'),
				'description' => $this->input->post('description'),
				'title' => $this->input->post('title'),
				'date' => $this->input->post('date'),
				'time' => $this->input->post('time'),
				'url' => $this->input->post('url')
			);

			$this->load->model('calendar_model');
			$error = $this->calendar_model->update_event($data);
				
			if ($error != ''){
				redirect('/calendar/edit' .$data['id']);
				echo $error;
			}
				
			redirect('/calendar');
		} else {
			redirect('/calendar/edit' .$data['id']);
		}

	}

	/**
	 *
	 * Redirects to the calendar if successful, loads error message otherwise
	 */
	function create_event(){
		if ($this->validate_event()){
			$data = array (
			'description' => $this->input->post('description', true),
			'title' => $this->input->post('title', true),
			'date' => $this->input->post('date', true),
			'time' => $this->input->post('time'), true);

			$this->load->model('calendar_model');
			$submit = $this->calendar_model->add_event($data);

			if (mysql_error() == ''){
				redirect('calendar');
			} else {
				$this->add_event();
			}
		} else {
			$this->add_event();
		}
	}

	/**
	 * 	Validates the form used to enter the new event information
	 *
	 */
	function validate_event(){
		$this->load->library('form_validation');

		$this->form_validation->set_rules('title', 'Title', 'trim|required|alpha_dash');
		$this->form_validation->set_rules('date', 'Date', 'trim|required|alpha_dash|callback_check_date');
		$this->form_validation->set_rules('time', 'Time', 'trim|alpha_dash');
		$this->form_validation->set_rules('description', 'Description','trim|required');

		return $this->form_validation->run();
	}

	/**
	 * This function checks the provided date to make sure that it is in the future
	 * */
	function check_date($str){
		$date_format = '%Y-%m-%e';
		$this->load->helper('date');
		$date = strptime($str, $date_format);
		if ($date == FALSE){
			$this->form_validation->set_message('check_date', 'Invalid %s Entered');
			return FALSE;
		} else if (strtotime($str) < now()){
			$this->form_validation->set_message('check_date', 'Date has to be in the future');
			return FAlSE;
		} else {
			return TRUE;
		}
	}

}
